package com.frank.im.utils;

import android.content.Context;

import com.frank.im.base.ImException;
import com.frank.im.bean.GetUserInfo;
import com.frank.im.bean.ImReqData;
import com.frank.im.bean.MarkReadMsg;
import com.frank.im.bean.MarkReadSysMsg;
import com.frank.im.bean.SendMsg;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * im数据转换类
 * 
 * @author Frank
 * @version 1.0 Create by 2015.4.8
 */
public class ImDataUtils {
	/**
	 * 获得初始化请求数据 json
	 * 
	 * @param context
	 * @param requestId
	 * @return
	 */
	public static String getInitData(Context context, String requestId) {
		if (context == null) {
			try {
				throw new ImException("context==null 上下文不能为空！");
			} catch (ImException e) {
				e.printStackTrace();
			}
		}
		if (requestId == null) {
			try {
				throw new ImException("requestId==null 请求id不能为空！");
			} catch (ImException e) {
				e.printStackTrace();
			}
		}
		return new Gson().toJson(ImEncrypUtils.getInitData(context, requestId),
				new TypeToken<ImReqData>() {
				}.getType());
	}

	/**
	 * 获得发送消息请求数据 json
	 * 
	 * @param context
	 * @param requestId
	 * @return
	 */
	public static String getSendContent(Context context, String requestId,
			String type, String content) {
		if (context == null) {
			try {
				throw new ImException("context==null 上下文不能为空！");
			} catch (ImException e) {
				e.printStackTrace();
			}
		}
		if (requestId == null) {
			try {
				throw new ImException("requestId==null 请求id不能为空！");
			} catch (ImException e) {
				e.printStackTrace();
			}
		}
		return new Gson()
				.toJson(ImEncrypUtils.getSendContent(context, requestId, type,
						content), new TypeToken<ImReqData>() {
				}.getType());
	}

	/**
	 * 获得发送消息 json
	 * 
	 * @param context
	 * @param requestId
	 * @return
	 */
	public static String getSendMsg(SendMsg sendMsg) {
		if (sendMsg == null) {
			try {
				throw new ImException("sendMsg==null 发送消息不能为空！");
			} catch (ImException e) {
				e.printStackTrace();
			}
		}
		return new Gson().toJson(sendMsg, new TypeToken<SendMsg>() {
		}.getType());
	}
	
	/**
	 * 获得标志系统消息已读 json
	 * 
	 * @param context
	 * @param requestId
	 * @return
	 */
	public static String getSendMsg(MarkReadSysMsg markReadSysMsg) {
		if (markReadSysMsg == null) {
			try {
				throw new ImException("markReadSysMsg==null 发送消息不能为空！");
			} catch (ImException e) {
				e.printStackTrace();
			}
		}
		return new Gson().toJson(markReadSysMsg, new TypeToken<MarkReadSysMsg>() {
		}.getType());
	}

	/**
	 * 获得标志消息已读 json
	 * 
	 * @param context
	 * @param requestId
	 * @return
	 */
	public static String getSendMsg(MarkReadMsg markReadMsg) {
		if (markReadMsg == null) {
			try {
				throw new ImException("markReadMsg==null 发送消息不能为空！");
			} catch (ImException e) {
				e.printStackTrace();
			}
		}
		return new Gson().toJson(markReadMsg, new TypeToken<MarkReadMsg>() {
		}.getType());
	}

	/**
	 * 获得获取用户信息 json
	 * 
	 * @param context
	 * @param requestId
	 * @return
	 */
	public static String getUserInfo(GetUserInfo getUserInfo) {
		if (getUserInfo == null) {
			try {
				throw new ImException("getUserInfo==null 获取用户信息不能为空！");
			} catch (ImException e) {
				e.printStackTrace();
			}
		}
		return new Gson().toJson(getUserInfo, new TypeToken<GetUserInfo>() {
		}.getType());
	}

}
